package com.hnhegui.backend.order.controller;


import com.hnhegui.backend.entity.PageResult;
import com.hnhegui.backend.entity.Result;
import com.hnhegui.backend.order.api.OrderRestApi;
import com.hnhegui.backend.order.convert.OrderConvert;
import com.hnhegui.backend.order.dto.request.AdminOrdersRequestDTO;
import com.hnhegui.backend.order.dto.request.CreateOrderRequestDTO;
import com.hnhegui.backend.order.dto.request.OrderDocumentPageRequestDTO;
import com.hnhegui.backend.order.dto.request.PayOrderRequestDTO;
import com.hnhegui.backend.order.dto.request.PaymentOrderCallbackRequestDTO;
import com.hnhegui.backend.order.dto.response.CreateOrderResponseDTO;
import com.hnhegui.backend.order.dto.response.OrderDocumentResponseDTO;
import com.hnhegui.backend.order.dto.response.PayOrderResponseDTO;
import com.hnhegui.backend.order.dto.response.PaymentOrderCallbackResponseDTO;
import com.hnhegui.backend.order.entity.OrderDocument;
import com.hnhegui.backend.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 订单Controller
 */
@RestController
@RequestMapping("/api/orders")
public class OrderController implements OrderRestApi {

    @Autowired
    private OrderService orderService;

    @Override
    public Result<CreateOrderResponseDTO> createOrder(CreateOrderRequestDTO request) {
        CreateOrderResponseDTO response = orderService.createOrder(request);
        return Result.success(response);
    }

    @Override
    public Result<PageResult<OrderDocumentResponseDTO>> getMyOrders(OrderDocumentPageRequestDTO request) {
        Page<OrderDocument> orderPage = orderService.getUserOrders(request);
        List<OrderDocument> documentList = orderPage.getContent();
        return Result.success(new PageResult<>(orderPage.getTotalElements(), documentList.stream().map(OrderConvert.INSTANCE::convertToDocument).toList()));
    }

    @Override
    public Result<OrderDocumentResponseDTO> getOrderDetail(String orderId) {
        OrderDocument order = orderService.getOrderById(orderId);
        return Result.success(OrderConvert.INSTANCE.convertToDocument(order));
    }

    @Override
    public Result<PageResult<OrderDocumentResponseDTO>> getAdminOrders(AdminOrdersRequestDTO request) {
        Page<OrderDocument> orderDocuments = orderService.searchAdminOrders(request);
        List<OrderDocument> documentList = orderDocuments.getContent();
        return Result.success(new PageResult<>(orderDocuments.getTotalElements(), documentList.stream().map(OrderConvert.INSTANCE::convertToDocument).toList()));
    }

    @Override
    public Result<PayOrderResponseDTO> payOrder(PayOrderRequestDTO request) {
        PayOrderResponseDTO response = orderService.payOrder(request);
        return Result.success(response);
    }

    @Override
    public Result<PaymentOrderCallbackResponseDTO> paymentCallback(PaymentOrderCallbackRequestDTO request) {
        PaymentOrderCallbackResponseDTO response =orderService.paymentCallback(request);
        return Result.success(response);
    }
}
